﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Region.Reports
{
    [Processor(TemplateClass="Nadzor1_5")]
    class Nadzor1_5
    {
        #region Справочники
        static Dictionary<Int32, Boolean> MeasureAll = new Dictionary<int, bool>() {
            {0,true},   // Не указанно
            {1,true},   // Штраф
            {2,true},   // Предупреждение
            {3,true},   // устное замечание     	
            {4,true},   // направлено в суд     
            {5,false},  // направлено в прокуратуру
            {6,false},  // дело прекращено
            {7,false},  // возврат
            {9,false},  // Объединено
            {10,false}, // по подведомственности
        };
        static Dictionary<Int32, Boolean> MeasureSudAll = new Dictionary<int, bool>() {
            {0, false}, // Не указанно
            {1, true},  // Предупреждение
            {2, true},  // Дисквалификация
            {3, true},  // Арест
            {4, true},  // Адм. штраф
            {5, true},  // Адм. штраф с конфискацией
            {6, true},  // Приостановление деятельности
            {7, true},  // Приостановление деятельности с конфискацией
            {8, false}, // прекращено
            {9, false}, // отменено
            {10,true},  // устное замечание
        };
        static Dictionary<String, String> colArticle = new Dictionary<String, String>() { 
            { "03", "K.06.01." },   { "04", "K.06.03." },   { "05", "K.06.04." },   { "06", "K.06.05." },
            { "07", "K.06.06." },   { "08", "K.06.07.p1." },{ "09", "K.06.07.p2." },{ "10", "K.06.17.p1." },
            { "11", "K.06.24.p1." },{ "12", "K.06.24.p2." },{ "13", "K.06.25.p1." },{ "14", "K.06.25.p2." },
            { "15", "K.06.25.p3." },{ "16", "K.06.27.p1." },{ "17", "K.06.27.p2." },{ "18", "K.06.33.p1." },
            { "19", "K.06.33.p2." },{ "20", "K.07.02.p2." },{ "21", "K.08.02." },   { "22", "K.08.05." },
            { "23", "K.08.42.p2." },{ "24", "K.09.16.p1." },{ "25", "K.09.16.p2." },{ "26", "K.10.08.p1." },
            { "27", "K.14.01.p2." },{ "28", "K.14.01.p3." },{ "29", "K.14.01.p4." },{ "30", "K.14.02." },
            { "31", "K.14.03.01.p1." },{ "32", "K.14.04.p1." },{ "33", "K.14.04.p2." },{ "34", "K.14.05.p1." },
            { "35", "K.14.06.p1." },{ "36", "K.14.06.p2." },{ "37", "K.14.07.p1." },{ "38", "K.14.07.p2." },
            { "39", "K.14.08.p1." },{ "40", "K.14.08.p2." },{ "41", "K.14.08.p3." },{ "42", "K.14.08.p4." },
            { "43", "K.14.10.p1." },{ "44", "K.14.10.p2." },{ "45", "K.14.15." },   { "46", "K.14.16.p2.1." },
            { "47", "K.14.16.p3." },{ "48", "K.14.26." },   { "49", "K.14.34.p1." },{ "50", "K.14.34.p2." },
            { "51", "K.14.34.p3." },{ "52", "K.14.34.p4." },{ "53", "K.14.34.p5." },{ "54", "K.14.34.p6." },
            { "55", "K.14.34.p7." },{ "56", "K.14.34.p8." },{ "57", "K.14.39." },   { "58", "K.14.43.p1." },
            { "59", "K.14.43.p2." },{ "60", "K.14.43.p3." },{ "61", "K.14.44.p1." },{ "62", "K.14.44.p2." },
            { "63", "K.14.44.p3." },{ "64", "K.14.45." },   { "65", "K.14.46.p1." },{ "66", "K.14.46.p2." },
            { "67", "K.14.46.01." },{ "68", "K.14.52.p1." },{ "69", "K.14.53.p1." },{ "70", "K.14.53.p2." },            
            { "71", "K.14.53.p3." },{ "72", "K.15.12.p1." },{ "73", "K.15.12.p2." },{ "74", "K.15.12.p3." },
            { "75", "K.15.12.p4." },{ "76", "K.17.07." },   { "77", "K.17.09." },   { "78", "K.18.17.p1." },
            { "79", "K.19.04.p1." },{ "80", "K.19.04.01.p1." },{ "81", "K.19.04.01.p2." },{ "82", "K.19.04.01.p3." },
            { "83", "K.19.05.p1." },{ "84", "K.19.05.p15." },{ "85", "K.19.05.p16." },{ "86", "K.19.06." },
            { "87", "K.19.07." },   { "88", "K.19.07.5-1.p1." },{ "89", "K.19.07.5-1.p2." },{ "90", "K.19.14." },
            { "91", "K.19.20.p1." },{ "92", "K.19.20.p2." },{ "93", "K.19.20.p3." },{ "94", "K.19.26.p1." },
            { "95", "K.19.33." },   { "96", "K.20.25.p1." },
        };
        #endregion
        #region Загрузка данных
        [ReaderList]
        public static void loaderRaspor(List<Raspor> list, Report report)
        {
            var l = new RasporList(report.Engine);
            l.Filter();
            //m_engine.preLoadRaspor(l);
            for (Int32 i = 0; i < l.NumLines; i++)
                list.Add(report.Engine.getRaspor(l.getLine(i).RasporID));
        }
        [ReaderList]
        public static void loaderProtokol(List<Protokol> list, Report report)
        {
            var l = new ProtokolList(report.Engine);
            if (!report.Start.Empty)
                l.setWhere("ProtokolCreateStart", report.Start.Date);
            if (!report.End.Empty)
                l.setWhere("ProtokolCreateEnd", report.End.Date);
            l.Filter();
            //m_engine.preLoadRaspor(l);
            for (Int32 i = 0; i < l.NumLines; i++)
                list.Add(report.Engine.getProtokol(l.getLine(i).ProtokolID));
        }
        #endregion
        #region Проверка данных
        // Проверяет протокол на соответствие отчёту 
        [CheckerCell]
        public static bool protokol(Protokol obj,Report report)
        {
            //Проверка на заполненость
            if (obj.ArticleID==0)
                return false;
            if (obj.ProtokolCreate.Empty)
                return false;//Нет Даты составления
            if (obj.ProtokolReg.Empty)
                return false;//Нет Даты регистрации
            if (obj.ProtokolView.Empty)
                return false;//Нет Даты расмотрения
            if (!MeasureAll[obj.MeasureID])
                return false;// Нет принятого решения
            if (obj.MeasureID==4 && !MeasureSudAll[obj.MeasureSudID]) 
                return false;// Нет принятого решения судом
            //Проверка на соответствие периоду отчёта
            if (obj.ProtokolCreate < report.Start)
                return false;
            if (obj.ProtokolCreate >= report.End)
                return false;
            if (report.OtdelID != 0 && report.OtdelID != obj.OID)
                return false;
            return true;
        }
        #endregion
        #region Заголовки
        static Dictionary<String, HeaderInfo> ColumnNames = null;
        [ColHeader(Table = "1")]
        public static IHeaderInfo HeaderColT1(String Col, Report report)
        {
            if (ColumnNames == null)
            {
                var newColumnNames = new Dictionary<String, HeaderInfo>();
                var ArticleNum = report.Engine.ArticleNum;
                var Article = report.Engine.Article;
                foreach (var cA in colArticle)
                {
                    Int32 ArticleID = ArticleNum.First((i) => i.Value == cA.Value).Key;                    
                    var hip = new HeaderInfo();
                    hip.ParentName = "Articles";
                    hip.Title = Article[ArticleID];
                    hip.Name = cA.Key + "p";
                    newColumnNames[cA.Key + "p"] = hip;
                    var hi = new HeaderInfo();
                    hi.ParentName = cA.Key + "p";
                    hi.Title = cA.Key;
                    hi.Name = cA.Key;
                    newColumnNames[cA.Key] = hi;
                }
                ColumnNames = newColumnNames;
            }
            if (ColumnNames.ContainsKey(Col))
                return ColumnNames[Col];            
            return null;
        }
        [ColHeader(Table = "1", Col = "Articles")]
        public static IHeaderInfo HeaderColT1Articles(String Col, Report report)
        {
            var hi = new HeaderInfo();
            hi.ParentName = null;
            hi.Title = "Статьи";
            hi.Name = Col;
            return hi;
        }
        [ColHeader(Table = "1", Col = "97p")]
        public static IHeaderInfo HeaderColT1С97p(String Col, Report report)
        {
            var hi = new HeaderInfo();
            hi.ParentName = null;
            hi.Title = "Итого(Сумма 03-96)";
            hi.Name = Col;
            return hi;
        }
        [ColHeader(Table = "1", Col = "97")]
        public static IHeaderInfo HeaderColT1С97(String Col, Report report)
        {
            var hi = new HeaderInfo();
            hi.ParentName = "97p";
            hi.Title = "97";
            hi.Name = Col;
            return hi;
        }

        static Dictionary<String, String> RowsNames = new Dictionary<String, String>() {
        //{"01p",""}
        };

        [RowHeader(Table = "1")]
        public static IHeaderInfo HeaderRowT1(String Row, Report report)
        {
            var hi = new HeaderInfo();
            if (RowsNames.ContainsKey(Row))
            {
                hi.ParentName = null;
                hi.Title = RowsNames[Row];
            }
            else if (!Row.EndsWith("p"))
            {
                hi.ParentName = Row + "p";
                hi.Title = Row;
            }
            else
            {
                hi.ParentName = null;
                hi.Title = Row;
            }
            hi.Name = Row;
            
            return hi;
        }

        #endregion
        #region Условия столбцов
        [CheckerCell(Table = "1", ColList = new String[] {  "03", "04", "05", "06", "07", "08", "09", 
            "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
            "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
            "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", 
            "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 
            "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
            "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
            "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 
            "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", 
            "90", "91", "92", "93", "94", "95", "96", 
        })]
        public static bool protokolT1Col03_96(Protokol obj, Report report, String Col)
        {
            return report.Engine.ArticleNum[obj.ArticleID] == colArticle[Col];
        }
        [CheckerCell(Table = "1", Col ="97")]
        public static bool protokolT1Col97(Protokol obj, Report report)
        {
            return true;
        }
        #endregion
        #region Условия строк
        [CheckerCell(Table = "1", RowList = new String[] { "01", "02" })]
        public static bool protokolT1R01_02(Protokol obj, Report report, String Row)
        {
            return true;
        }
        [CheckerCell(Table = "1", RowList = new String[] { "03",  "04", "05", "06" })]
        public static bool protokolT1R03_06(Protokol obj, Report report, String Row)
        {
            switch (Row)
            {
                case "03": return obj.FromTypeID == 3;
                case "04": return obj.FromTypeID == 2;
                case "05": return obj.FromTypeID == 4;
                case "06": return obj.FromTypeID == 1;
            }
            return false;
        }
        #endregion
        #region Вывод результатов
        // если не задано вычисление для ячеек строк и столбцов то вычисляем для страницы
        [ResultCellAttribute(Table = "1")]
        public static String protokolResultT1(IResultDataInfo obj, Report report)
        {
            List<Protokol> list = obj.getList<Protokol>();
            if (list == null)
                return "";
            return list.Count.ToString();
        }
        [ResultCellAttribute(Table = "1", Row = "01")]
        public static String protokolResultT1R01(IResultDataInfo obj, Report report)
        {
            List<Protokol> list = obj.getList<Protokol>();
            if (list == null)
                return "";
            return (list.Count > 0) ? "1" : "0";
        }
        [ResultCellAttribute(Table = "1", Row = "01", Col = "97")]
        public static String protokolResultT1R01C97(IResultDataInfo obj, Report report)
        {
            List<Protokol> list = obj.getList<Protokol>();
            if (list == null)
                return "";
            var Articles = new Dictionary<Int32, Boolean>();
            foreach(var l in list)
                Articles[l.ArticleID] = true;
            return Articles.Count.ToString();
        }

        //Объевляем столбцы но не вычисляем их(см protokolResultT1)
        [ResultCellAttribute(Table = "1", ColList = new String[] { "03", "04", "05", "06", "07", "08", "09", 
            "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
            "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
            "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", 
            "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 
            "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
            "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
            "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 
            "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", 
            "90", "91", "92", "93", "94", "95", "96", 
            "97" })]
        public static String protokolResultT1Col(IResultDataInfo obj, Report report, String Col)
        {
            return null;
        }
        //Объевляем строки но не вычисляем их(см protokolResultT1)
        [ResultCellAttribute(Table = "1", RowList = new String[] { "02", "03", "04", "05", "06" })]
        public static String protokolResultT1Row(IResultDataInfo obj, Report report, String Row)
        {
            return null;
        }
        #endregion
    }
}
